package fsfileserver;

import com.thoughtworks.xstream.*;
import com.thoughtworks.xstream.io.xml.DomDriver;
import java.io.*;
import java.util.ArrayList;

public class FileLoader  {
    private XStream xstream;

    public FileLoader(){
        xstream = new XStream(new DomDriver());
        xstream.alias("File", File.class);
        xstream.alias("FileList", ArrayList.class);
    }

    public ArrayList<File> LoadFile(String path){
        try{
            System.out.println("Loading filelist from: "+path+" ...");
            FileInputStream istream = new FileInputStream(path);

            ArrayList<File> tempFileList = (ArrayList<File>) xstream.fromXML(istream);
            for(File file:tempFileList){
                System.out.println("-> "+file.getName()+" (needs "+file.getBandwidth()+"Kb/s)");
            }
            return tempFileList;
        }
        catch(Exception ex){
            ex.printStackTrace();
            System.exit(1);
        }
        return null;
    }

    public void SaveFile(String path, ArrayList<File> fileList){
        try{
            FileOutputStream ostream = new FileOutputStream(path);
            xstream.toXML(fileList,ostream);
        }
        catch(Exception ex){
            ex.printStackTrace();
            System.exit(2);
        }
    }
}
